package com.team7.happycommunity.businessservice.controller;

import com.team7.happycommunity.businessservice.common.CommonResult;
import com.team7.happycommunity.businessservice.entity.*;
import com.team7.happycommunity.businessservice.service.PpPayserviceService;
import com.team7.happycommunity.businessservice.service.ServiceImgService;
import com.team7.happycommunity.businessservice.util.QiniuUploadUtil;
import io.swagger.annotations.*;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * (PpPayservice)表控制层
 *
 * @author makejava
 * @since 2020-03-26 21:40:52
 */
@RestController
@RequestMapping("ppPayservice")
@Api(tags="服务接口")
public class PpPayserviceController {
    /**
     * 服务对象
     */
    @Resource
    private PpPayserviceService ppPayserviceService;

    @Resource
    private HttpServletRequest request;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private ServiceImgService serviceImgService;



    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public PpPayservice selectOne(Integer id) {
        return this.ppPayserviceService.queryById(id);
    }

    /**
     * 数据上传，商家发布服务
     * @param ppPayservice 发布服务的信息，
     * @param
     * @return
     */
    @PostMapping("/addService")
    @ResponseBody
    @ApiOperation(value = "商家发布服务")

    @ApiResponses(value = {
            @ApiResponse(code = 200,message = "发布成功"),
            @ApiResponse(code = 500,message = "发布失败")
    })
    public CommonResult addService(PpPayservice ppPayservice){


        String userId = request.getHeader("userId");
        try {
            //存储登录的商家id
            ppPayservice.setShopid(Integer.valueOf(userId));
            //存储服务类型
            ppPayservice.setServiceType(0);
            //保存服务
            PpPayservice insert = ppPayserviceService.insert(ppPayservice);

            List<Object> hashList = redisTemplate.opsForHash().values(userId+"");
            ArrayList list=new ArrayList();
            ServiceImg img=new ServiceImg();
            for (Object o : hashList) {
                System.out.println(o);
                img.setImg(String.valueOf(o));
                img.setServiceid(insert.getId());
                list.add(img);
            }
            //使用foreach插入多条数据
           //redisTemplate.opsForHash().delete(userId+"");
            serviceImgService.insertList(list);
             //返回添加的服务id
            return CommonResult.success("发布成功");
        } catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("发布失败");
        }
    }

    /**
     * 查询商家的所有收益
     * @param userId 商家id
     * @return
     */
    @RequestMapping("/queryEarnByUserId")
    @ResponseBody
    @ApiOperation(value = "查询商家的所有收益")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userId",value = "商家id",required = true),
    })

    public List<EarnIng> queryEarnByUserId(Integer userId){
        try {
            List<EarnIng> list=ppPayserviceService.queryEarnByUserId(userId);
            System.out.println(list);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 查询商家发布的服务，并根据时间排序
     * @param userId 用户id
     * @return
     */
    @GetMapping("/queryServicedByUserId")
    @ApiOperation(value = "查询商家发布的服务")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "page",value = "页数",required = true),
            @ApiImplicitParam(name = "limit",value = "每页条数",required = true)
    })
    public PageInfo<List<PpPayservice>> queryServicedByUserId(Integer userId, Integer page, Integer limit){


        //查询总条数
        Integer count=ppPayserviceService.queryCountByUserId(userId);
        if (page==null||page.equals("")||page<=0){
            page=1;
        }
        if(page>count/limit){
             page =(int)Math.ceil(count /Double.valueOf(limit)) ;
        }
        if (limit==null||limit.equals("")){
            limit=3;
        }
        Integer start=(page-1)*limit;

        List<PpPayservice> list=ppPayserviceService.queryServicedByUserId(start,limit,userId);
        PageInfo<List<PpPayservice>> info=new PageInfo<List<PpPayservice>>(list);
        info.setLimit(limit);
        info.setPage(page);
        info.setCount(count);
        return info;
    }

    /**
     * 修改服务状态或删除服务
     * @param status 服务状态，当传输值为2时，删除服务
     * @param id 服务的id
     * @return
     */
    @GetMapping("/alterStatus")
    @ApiOperation(value = "修改服务状态或删除服务")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "status",value = "登录的手机号或者邮箱",required = true),
            @ApiImplicitParam(name = "id",value = "用户id",required = true)
    })

    @ApiResponses(value = {
            @ApiResponse(code = 200,message = "修改成功"),
            @ApiResponse(code = 500,message = "修改失败")
    })
    public CommonResult alterStatus(Integer status,Integer id){
        try {
            //删除服务
            if(status==2){
                ppPayserviceService.deleteById(id);
                //根据服务的id删除服务的图片
                serviceImgService.deleteByServiceId(id);
              }
            if(status==0||status==1){
              //修改服务的状态
                ppPayserviceService.updateStatusById(status,id);
            }
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("修改失败");
        }

    }

    /**
     * 根据服务的id查询服务的详细信息
     * @param id 服务id
     * @return 服务详情
     */
    @GetMapping("/queryDetailsById")
    @ApiOperation(value = "查询服务的详细信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "用户id",required = true)
    })
    public PpPayservice queryDetailsById(Integer id){
       return ppPayserviceService.queryDetailsById(id);
    }

    public static void main(String[] args) {
        System.out.println(Math.ceil(10/Double.valueOf(3)));
    }
}